package com.hww.v2_mtfsdk.utils;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hww.common.MyAppThis;
import com.hww.common.util.DialogUtils;
import com.hww.common.util.SharePreferencesUtils;
import com.hww.common.util.StringUtils;
import com.hww.common.util.ThreadUtils;
import com.hww.common.util.ToastUtils;
import com.hww.v2_mtfsdk.Constant;
import com.hww.v2_mtfsdk.R;

/**
 * @author hww
 * @date 0011 2024/7/11
 * email: heww@zhongyuntech.cn
 * description:
 */
public class DialogCustom {

    public static void showDialog(final Activity activity) {
        final View viewFromResource = DialogUtils.getViewFromResource(activity, R.layout.custom_dialog_layout);
        final Dialog dialogDefaultScreen = DialogUtils.createDialogDefaultScreen(activity, viewFromResource);
        dialogDefaultScreen.show();
        int sCameraId = SharePreferencesUtils.getInt(MyAppThis.getThis(), Constant.SHARE_CAMERA_ID, 0);
        int sCameraP = SharePreferencesUtils.getInt(MyAppThis.getThis(), Constant.SHARE_ANGLE_PREVIEW, 0);
        int sCameraC = SharePreferencesUtils.getInt(MyAppThis.getThis(), Constant.SHARE_ANGLE_CALL, 0);
        boolean sFace = SharePreferencesUtils.getBoolean(MyAppThis.getThis(), Constant.SHARE_FACE_MIRRORING, false);
        float sSim = (float) SharePreferencesUtils.get(MyAppThis.getThis(), Constant.SHARE_FACE_SIMILARITY, 0.65f);
        TextView tvTitle = viewFromResource.findViewById(R.id.custom_dialog_title);
        tvTitle.setText("设置");
        EditText edCameraId = viewFromResource.findViewById(R.id.custom_dialog_edit_camera_id);
        edCameraId.setHint("摄像头ID");
        edCameraId.setText(String.valueOf(sCameraId));
        EditText edCameraP = viewFromResource.findViewById(R.id.custom_dialog_edit_camera_angle_preview);
        edCameraP.setHint("摄像头预览角度");
        edCameraP.setText(String.valueOf(sCameraP));
        EditText edCameraC = viewFromResource.findViewById(R.id.custom_dialog_edit_camera_angle_call);
        edCameraC.setHint("摄像头回调数据旋转角度");
        edCameraC.setText(String.valueOf(sCameraC));
        EditText edFace = viewFromResource.findViewById(R.id.custom_dialog_edit_face_mark);
        edFace.setText(String.valueOf(sFace));
        EditText edSim = viewFromResource.findViewById(R.id.custom_dialog_edit_similarity_threshold);
        edSim.setText(String.valueOf(sSim));

        TextView tvCameraId = viewFromResource.findViewById(R.id.custom_dialog_tv_camera_id);
        tvCameraId.setText("摄像头ID");
        TextView tvCameraP = viewFromResource.findViewById(R.id.custom_dialog_tv_camera_angle_preview);
        tvCameraP.setText("预览角度");
        TextView tvCameraC = viewFromResource.findViewById(R.id.custom_dialog_tv_camera_angle_call);
        tvCameraC.setText("回调角度");
        TextView tvFaceMark = viewFromResource.findViewById(R.id.custom_dialog_tv_face_mark);
        tvFaceMark.setText("人脸框镜像");
        TextView tvSim = viewFromResource.findViewById(R.id.custom_dialog_tv_similarity_threshold);
        tvSim.setText("阈值设置");

        Button btPositive = viewFromResource.findViewById(R.id.custom_dialog_common_positive);
        btPositive.setText("设置");
        Button btNegative = viewFromResource.findViewById(R.id.custom_dialog_common_negative);
        btNegative.setText("取消");

        btPositive.setOnClickListener((view) -> {
            try {
                String cameraId = edCameraId.getText().toString().trim();
                String preview = edCameraP.getText().toString().trim();
                String call = edCameraC.getText().toString().trim();
                String isMirroring = edFace.getText().toString().trim();
                String similarity = edSim.getText().toString().trim();
                if (StringUtils.isEmpty(cameraId)) {
                    mToast(false, "请输入相机ID");
                    return;
                }
                if (StringUtils.isEmpty(preview)) {
                    mToast(false, "请输入预览角度");
                    return;
                }
                if (StringUtils.isEmpty(call)) {
                    mToast(false, "请输入摄像头回调数据需要旋转的角度");
                    return;
                }
                if (StringUtils.isEmpty(isMirroring)) {
                    mToast(false, "请输入是否镜像");
                    return;
                }
                if (!("true".equals(isMirroring) || "false".equals(isMirroring))) {
                    mToast(false, "人脸框镜像配置为 true 或 false");
                    return;
                }
                if (StringUtils.isEmpty(similarity)) {
                    mToast(false, "请输入相似度阈值");
                    return;
                }
                boolean a = SharePreferencesUtils.putInt(MyAppThis.getThis(), Constant.SHARE_CAMERA_ID, Integer.parseInt(cameraId));
                boolean b = SharePreferencesUtils.putInt(MyAppThis.getThis(), Constant.SHARE_ANGLE_PREVIEW, Integer.parseInt(preview));
                boolean c = SharePreferencesUtils.putInt(MyAppThis.getThis(), Constant.SHARE_ANGLE_CALL, Integer.parseInt(call));
                boolean d = SharePreferencesUtils.putBoolean(MyAppThis.getThis(), Constant.SHARE_FACE_MIRRORING, "true".equals(isMirroring));
                boolean e = SharePreferencesUtils.put(MyAppThis.getThis(), Constant.SHARE_FACE_SIMILARITY, Float.valueOf(similarity));
                if (a && b && c && d && e) {
                    mToast(true, "保存成功");
                } else {
                    mToast(false, "保存失败:" + (a ? "" : "cameraId") + (b ? "" : "anglePreview")
                            + (c ? "" : "angleCall") + (d ? "" : "faceMirroring") + (e ? "" : "faceSimilarity"));
                }
                dialogDefaultScreen.dismiss();
            } catch (Exception e) {
                mToast(false, e.getMessage());
            }
        });
        btNegative.setOnClickListener((view) -> {
            dialogDefaultScreen.dismiss();
        });
    }

    private static void mToast(boolean status, String msg) {
        ThreadUtils.runOnUiThread(() -> {
            if (status) {
                ToastUtils.success(MyAppThis.getThis(), msg).show();
            } else {
                ToastUtils.error(MyAppThis.getThis(), msg).show();
            }
        });
    }

}
